package com.deep.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.deep.product.model.entity.BrandEntity;
import com.deep.product.service.BrandService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.deep.common.utils.PageUtils;
import com.deep.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author deep
 * @email ${email}
 * @date 2022-01-13 17:09:54
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:brand:save")
    public R save(@RequestBody @Valid BrandEntity brand) {      // 进行校验
        brandService.save(brand);

        return R.ok();
    }

    /*@RequestMapping("/save")
    @RequiresPermissions("product:brand:save")
    public R save(@RequestBody @Validated({AddGroup.class}) BrandEntity brand) {    // 进行组检验
        brandService.save(brand);

        return R.ok();
    }*/
    /*@RequestMapping("/save")
    @RequiresPermissions("product:brand:save")
    public R save(@RequestBody @Valid BrandEntity brand, BindingResult result) {     // @Valid开启校验，BindingResult校验错无信息返回
        if (result.hasErrors()) {
            Map<String, String> errorMap = new HashMap<>();
            result.getFieldErrors().forEach((item) -> {
                // 错误字段名
                String field = item.getField();
                // 错误详情
                String message = item.getDefaultMessage();
                errorMap.put(field, message);
            });
            return R.error(400,"提交信息不合法!").put("data", errorMap);
        }
        brandService.save(brand);
        return R.ok();
    }*/

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);
        // brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
